﻿using MQTTnet;
using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace Artizan.Iot.Hub.Mqtt.Server
{
    public class MqttServiceBase : IMqttServiceBase
    {
        public MqttServer MqttServer { get; private set; }

        public MqttServiceBase() { }

        public virtual void ConfigureMqttServer(MqttServer mqttServer)
        {
            MqttServer = mqttServer;
        }

        protected virtual async Task<MqttClientStatus?> GetClientStatusAsync(string clientId)
        {
            var allClientStatuses = await MqttServer.GetClientsAsync();
            return allClientStatuses.FirstOrDefault(cs => cs.Id == clientId);
        }

        protected virtual string GetClientIdFromPayload(MqttApplicationMessage message)
        {
            var payload = System.Text.Encoding.UTF8.GetString(message.Payload);
            // TODO: for JSON type data transfer get clientId from json payload
            return payload;
        }

        protected virtual async Task DisconnectClientAsync(string clientId)
        {
            var clientStatus = await GetClientStatusAsync(clientId);
            if (clientStatus != null)
            {
                await clientStatus.DisconnectAsync();
            }
        }

   
    }
}
